import { getSessionCookie } from 'better-auth/cookies'
import { NextResponse, type NextRequest } from 'next/server'

const protectedRoutes = ['/profile', '/post/create', '/post/edit']

export async function middleware(request: NextRequest) {
  const pathName = request.nextUrl.pathname

  const session = getSessionCookie(request)

  const isProtectedRoute = protectedRoutes.includes(pathName)

  if (isProtectedRoute && !session) {
    return NextResponse.redirect(new URL('/auth', request.url))
  }

  if (pathName === '/auth' && session) {
    return NextResponse.redirect(new URL('/', request.url))
  }

  return NextResponse.next()
}

export const config = {
  matcher: ['/profile/:path*', '/post/create', '/post/edit/:path*', '/auth']
}
